<?php

//use yii\web\View;

use yii\helpers\Url;

echo $this->render('/layouts/top');

?>
<script type="text/javascript" charset="utf-8" src="/js/ajax141212.js"></script>
<script type="text/javascript" src="/js/NoLimitCategory.js"></script>

<div style="margin:0px auto; width:1000px;">
	<div style=""><!--
 --><div style="display:inline-block;">分类：</div><!--
 --><div id="cont_category" style="display:inline-block;"><!--
--></div>
		<div style="margin:0px auto; width:1000px; margin-top:10px;">
        
<?php
	$word = isset($knowledge["word"]) ? $knowledge["word"] : "";
	$word = !empty($pre_word) ? $pre_word : $word;
?>
			单词：<input id="word" type="text" value="<?php echo $word?>" style="width:400px;" /> 
            <input type="button" value="有道抓取" onclick="getByYoudao()"/>
		</div>
		<div style="margin:0px auto; width:1000px; margin-top:10px;">
			音标：英<input id="ipa_en" type="text" value="<?php echo isset($knowledge["ipa_en"]) ? stripslashes($knowledge["ipa_en"]) : ""?>" style="width:150px;" />
			美<input id="ipa_us" type="text" value="<?php echo isset($knowledge["ipa_us"]) ? stripslashes($knowledge["ipa_us"]) : ""?>" style="width:150px;" />
		</div>
		<div style="margin:0px auto; width:1000px; margin-top:10px;">
			翻译：<input id="trans" type="text" value="<?php echo isset($knowledge["trans"]) ? $knowledge["trans"] : ""?>" style="width:400px;" />
		</div>
		<div style="margin:0px auto; width:1000px; margin-top:10px; text-align:center ">
          <input type="button" value="提交编辑" onclick="dataedit()"/> 
          <input type="button" value="预留按钮"/> 
        </div>
	</div>
</div>
<br>
<?php echo $this->render('/layouts/footer');?>
<script language="javascript">

var id = <?php echo isset($knowledge["id"]) ? $knowledge["id"] : 0;?>;
var c1 = <?php echo isset($knowledge["c1"]) ? $knowledge["c1"] : 0;?>;
var c2 = <?php echo isset($knowledge["c2"]) ? $knowledge["c2"] : 0;?>;
var c3 = <?php echo isset($knowledge["c3"]) ? $knowledge["c3"] : 0;?>;

var nlc = new NoLimitCategory(<?php echo json_encode($treeData, JSON_UNESCAPED_UNICODE)?>, document.getElementById("cont_category"), "全部");

nlc.onSelectCreate = function(selectObj, n){
	if(n == 1){
		selectObj.style.cssText = 'margin-left:0px;height:22px;';
	}else{
		selectObj.style.cssText = 'margin-left:5px;height:22px;';	
	}
};

var cids = [];
if(c1 != 0)cids.push(c1);
if(c2 != 0)cids.push(c2);
if(c3 != 0)cids.push(c3);

nlc.append(cids);


var dataedit = function(){

	var word = document.getElementById("word").value;
	var ipa_en = document.getElementById("ipa_en").value;
	var ipa_us = document.getElementById("ipa_us").value;
	var trans = document.getElementById("trans").value;

	var categorys = nlc.getSelects();
	var c1=0, c2=0, c3=0, cid = 0;
	for(var i=0; i<categorys.length; i++){
		if(i == 0)c1 = categorys[i].value;
		if(i == 1)c2 = categorys[i].value;
		if(i == 2)c3 = categorys[i].value;
		if(categorys[i].value > 0)cid = categorys[i].value;//所属分类为不为0的最后一个
	}

	new Ajax().post({
		url : "<?php echo Url::to(['/word/async-edit'])?>",
		data : "id="+id+"&c1="+c1+"&c2="+c2+"&c3="+c3+"&cid="+cid+"&word="+encodeURIComponent(word)+"&ipa_en="+encodeURIComponent(ipa_en)+"&ipa_us="+encodeURIComponent(ipa_us)
		+"&trans="+encodeURIComponent(trans),
		resultType : "json",
		onLoaded : function(result){
			console.log(result);
			if(result.code == 1){
				alert(result.info);
				
				id = (id>0) ? id : result.id;

				if(id >0){
					window.location.href = "/word-"+id+".html";
				}
				//window.location.href = "<?php echo Url::to(['/word/detail', 'id'=>$knowledge["id"]])?>";
			}else if(result.code == -2){
				alert(result.info);
				//window.location.href = "<?php echo Url::to(['/word/edit'])?>?id="+result.id;
				window.location.href = "/word-"+result.id+".html";
			}else{
				alert(result.info);
			}
			
		}
	});
}

//从有道抓取
function getByYoudao(){
	var word = document.getElementById("word").value;
	
	if(word == ""){
		alert("单词必填");return;
	}
	
	new Ajax().get({
		url : "<?php echo Url::to(['/word/insert-from-youdao'])?>"+"?word="+encodeURIComponent(word),
		resultType : "json",
		onLoaded : function(result){
			console.log(result);
			if(result.code == 1){
				console.log(result.data.trans);
				
				var word_youdao = result.data.word;
				if(result.data.word != ""){
					$('#word').val(result.data.word);
				}
				
				//翻译，用竖线隔开
				var trans_str = '';
				for(var i=0; i<result.data.trans.length; i++){
					trans_str += result.data.trans[i]+"|";
				}
				$('#trans').val(trans_str);
				
				var temp = '';
				for(var i=0; i<result.data.trans.length; i++){
					temp += " "+result.data.trans[i];
				}
				$('#question').val(temp);
				
				var ipa_en = (typeof(result.data.ipa[0]) == 'undefined') ? '' : result.data.ipa[0];
				var ipa_us = (typeof(result.data.ipa[1]) == 'undefined') ? '' : result.data.ipa[1];
				$('#ipa_en').val(ipa_en);
				$('#ipa_us').val(ipa_us);

			}else{
				alert(result.info);
			}
		}
	});
}

	
</script>
</body>
</html>